var User = require('../model/user').User
var UserInfo = require('../model/user').UserInfo
var User_info_pre = require('../model/user').User_info_pre
var Education_bg = require('../model/user').Education_bg
var Job = require('../model/user').Job
var Skill = require('../model/user').Skill
var Opus = require('../model/user').Opus
let Identify = require('../model/identify')
var Feedback = require('../model/feedback')
var TaskList = require('../model/taskList').TaskList
var TaskReply = require('../model/taskReply').TaskReply
var User_verify_progress = require('../model/user_verify_progress')
var User_preferences = require('../model/user_preferences')
const axios = require('axios')
const md5 = require('md5')
var validator = require('validator');
const result = require('../utils/result')
exports.user = async (request, response) => {
    // console.log(request.query.code)
    let code = request.query.code
    var url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx838cf54dd030d93a&secret=b7f8aca56652260930307a1597e349ab&code=' + code + '&grant_type=authorization_code'
    axios.get(url).then(r => {
        // console.log(r.data)
        // response.json(r.data)
        if (r.data !== 40163 && r.data.openid) {
            let openid = r.data.openid
            User.findOne({
                where: {
                    openid: openid
                }
            }).then(g => {
                if (g) {
                    User.update({ access_token: md5(new Date() + 'ZX') }, {
                        where: {
                            openid: openid
                        }
                    }).then(z => {
                        if (z) {
                            User.findOne({
                                where: {
                                    openid: openid
                                }
                            }).then(f => {
                                response.json({
                                    err_code: 0,
                                    err_msg: '成功',
                                    data: f
                                })
                            })
                        }
                    })
                } else {
                    let access_token = r.data.access_token
                    let url2 = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + access_token + '&openid=' + openid + '&lang=zh_CN'
                    axios.get(url2).then(y => {

                        let tmp = {
                            access_token: md5(new Date() + 'ZX'),
                            err_code: 0,
                            err_msg: '成功',
                        }
                        response.status(200)
                        response.json(Object.assign(tmp, y.data))
                        User.create({
                            openid: y.data.openid,
                            access_token: tmp.access_token,
                            nickname: y.data.nickname,
                            sex: y.data.sex,
                            country: y.data.country,
                            headimgurl: y.data.headimgurl
                        })
                    })
                }
            })
        }
    })

}

exports.taskList = async (req, res, next) => {
    return new Promise((resolve, reject) => {
        TaskList.create({
            name: 'nodejs',
            describe: 'miaoshu',
        })
    })
}
exports.identify = async (req, res) => {
    if (!req.query.uid) {
        res.json(result(-1, 'success', `缺少参数:uid`))
        return;
    }
    let a = ['name', 'sex', 'idNo']
    let b = ['姓名', '性别', '身份证']
    if (!req.query.name) {
        res.json(result(-1, 'success', `缺少参数:姓名`))
        return
    }
    if (!req.query.idNo) {
        res.json(result(-1, 'success', `缺少参数:身份证}`))
        return
    }
    let _gg = await Identify.findAll({ where: { uid: req.query.uid } })
    if (!_gg || _gg.length == 0) {
        Identify.create(req.query).then(r => {
            if (r) {
                res.json(result(0, 'success', '认证成功'))
            }
        }).catch(e => {
            res.json(result(-1, 'fail', '认证失败'))
        })
    } else {
        res.json(result(-1, 'fail', `你已认证`))
        return;
    }

}
exports.taskReply = async (req, res) => {
    // TaskReply
}
exports.user_verify_progress = async (req, res) => {
    // TaskReply
}
exports.user_preferences = async (req, res) => {
    let uid = req.query.uid;
    if (!uid) {
        res.json(result(-1, 'fail', '缺少参数uid'))
        return
    }
    if (!req.query.available_time) {
        res.json(result(-1, 'fail', '缺少参数available_time'))
        return
    }
    if (!req.query.skill) {
        res.json(result(-1, 'fail', '缺少参数skill'))
        return
    }
    if (!req.query.status) {
        res.json(result(-1, 'fail', '缺少参数status'))
        return
    }
    User_preferences.create({
        uid: uid,
        available_time: req.query.available_time,
        status: req.query.status,
        skill: req.query.skill
    }).then(v => {
        res.json(result(0, 'success', '成功'))
    }).catch(e => {
        res.json(result(0, 'success', '失败'))
    })
    // TaskReply
}

exports.mine = async (req, res) => {

    User.findOne({
        where: {
            uid: req.query.uid
        }
    }).then(r => {
        res.json(result(0, 'success', r))
    })
}
exports.Feedback = async (req, res) => {
    let uid = req.query.uid;
    let title = req.query.title;
    let content = req.query.content;
    let contactType = req.query.contactType;
    Feedback.create({
        uid, title, content, contactType
    }).then(v => {
        res.json(result(0, 'success', '操作成功'))
    }).catch(err => {
        res.json(result(-1, 'fail', '操作失败'))
    })
}
exports.UserList = async (req, res) => {
    User.findAll().then(r => {
        res.json(result(0, 'success', r))
    })
}
/**
 * 用户封禁
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.ban_user = async (req, res) => {
    let R = await User.update({
        ban: 1
    }, { where: { uid: req.query.uid } })
    if (R && R[0] !== 0) {
        res.json(result(0, 'success', '执行封禁成功'))
    } else {
        res.json(result(-1, 'fail', '封禁失败'))
    }

}

/**
 * 用户信息创建
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_info_create = async (req, res) => {
    let uid = req.query.uid;
   UserInfo.findOne({where:{uid:uid}}).then(v=>{
       UserInfo.update({
           uid: uid,
           name: req.query.name,
           age: req.query.age,
           asm: req.query.asm,
           sex: req.query.sex,
           subtitle: req.query.subtitle,
           email: req.query.email,
           phone: req.query.phone,
           wx_pay_no: req.query.wx_pay_no,
           ali_pay_no: req.query.ali_pay_no,
           school: req.query.school || null,
       },{where:{uid:uid}}).then(r => {
           res.json(result(0, 'success', '创建成功'))

       }).catch(e => {
           res.json(result(-1, 'fail', '创建失败'))
       })
   }).catch(e=>{
       UserInfo.create({
           uid: uid,
           name: req.query.name,
           age: req.query.age,
           asm: req.query.asm,
           sex: req.query.sex,
           subtitle: req.query.subtitle,
           email: req.query.email,
           phone: req.query.phone,
           wx_pay_no: req.query.wx_pay_no,
           ali_pay_no: req.query.ali_pay_no,
           school: req.query.school || null,
       }).then(r => {
           res.json(result(0, 'success', '创建成功'))

       }).catch(e => {
           res.json(result(-1, 'fail', '创建失败'))
       })
   })


}

/**
 * 用户信息查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_info = async (req, res) => {
    let uid = req.query.uid;
    if (!uid) {
        res.json(result(-1, 'fail', '缺少uid参数'))
        return
    }
    let eg = await UserInfo.findOne({ where: { uid: uid } })
    if (eg) {
        res.json(result(0, 'success', eg))
    } else {
        res.json(result(0, 'success', '创建失败'))
    }
}

/**
 * 教育背景创建
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_education_bg = async (req, res) => {
    let edId = req.query.edId;
    if(edId === "-1"){
        let uid = req.query.uid;
        if (!uid) {
            res.json(result(-1, 'fail', '缺少uid参数'))
            return
        }
        //let u = await User.findOne({where:{uid:uid}});
        //let eg = await Education_bg.findOne({where:{uid:uid}})
        Education_bg.create({
            uid: uid,
            school: req.query.school,
            speciality: req.query.speciality,
            academic: req.query.academic,
            honor: req.query.honor
        }).then(r => {
            res.json(result(0, 'success', '创建成功'))
        }).catch(e => {
            res.json(result(-1, 'fail', '创建失败'))
        })
    }
    else{
        Education_bg.update(
            {
                school: req.query.school,
                speciality: req.query.speciality,
                academic: req.query.academic,
                honor: req.query.honor
            }, { where: { id: edId } }
        ).then(r => {
            res.json(result(0, 'success', '修改成功'))
        }).catch(e => {
            res.json(result(-1, 'fail', '修改失败'))
        })
    }
}

/**
 * 教育背景查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_education_bg_show = async (req, res) => {
    let uid = req.query.uid;

    if (!uid) {
        res.json(result(-1, 'fail', '缺少uid参数'))
        return
    }
    let eg = await Education_bg.findAll({ where: { uid: uid } })
    if (eg) {
        res.json(result(0, 'success', eg))
    } else {
        res.json(result(0, 'success', '创建失败'))
    }
}

/**
 * 单项教育背景查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_education_bg_show_edId = async (req, res) => {
    let edId = req.query.edId;

    if (!edId) {
        res.json(result(-1, 'fail', '缺少edId参数'))
        return
    }
    let eg = await Education_bg.findOne({ where: { id: edId } })
    if (eg) {
        res.json(result(0, 'success', eg))
    } else {
        res.json(result(-1, 'fail', '查询失败'))
    }
}

/**
 * 工作经历创建
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_job_create = async (req, res) => {
    let workId = req.query.workId;
    if(workId === "-1"){
        let uid = req.query.uid;
        if (!uid) {
            res.json(result(-1, 'fail', '缺少uid参数'))
            return
        }
        Job.create({
            uid: uid,
            company: req.query.company,
            start_time: req.query.start_time,
            end_time: req.query.end_time,
            address: req.query.address,
            position: req.query.position,
            des: req.query.des
        }).then((r => {
            res.json(result(0, 'success', '创建成功'))
        })).catch(e => {
            res.json(result(-1, 'fail', '创建失败'))
        })
    }
    else{
        Job.update(
            {
                company: req.query.company,
                start_time: req.query.start_time,
                end_time: req.query.end_time,
                address: req.query.address,
                position: req.query.position,
                des: req.query.des
            }, { where: { id: workId } }
        ).then(r => {
            res.json(result(0, 'success', '修改成功'))
        }).catch(e => {
            res.json(result(-1, 'fail', '修改失败'))
        })
    }
}

/**
 * 工作经历查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_job_show = async (req, res) => {
    let uid = req.query.uid;
    if (!uid) {
        res.json(result(-1, 'fail', '缺少uid参数'))
        return
    }
    Job.findAll({ where: { uid: uid } }).then((r => {
        res.json(result(0, 'success', r))
    })).catch(e => {
        res.json(result(-1, 'fail', '创建失败'))
    })
}

/**
 * 单项工作经历查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_job_show_workId = async (req, res) => {
    let workId = req.query.workId;
    if (!workId) {
        res.json(result(-1, 'fail', '缺少workId参数'))
        return
    }
    Job.findOne({ where: { id: workId } }).then((r => {
        res.json(result(0, 'success', r))
    })).catch(e => {
        res.json(result(-1, 'fail', '查询失败'))
    })
}

/**
 * 工作经历删除
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.user_job_delete = async (req, res) => {
    let uid = req.query.uid;
    let id = req.query.id;
    if (!id) {
        res.json(result(-1, 'fail', '缺少id参数'))
        return
    }
    if (!uid) {
        res.json(result(-1, 'fail', '缺少uid参数'))
        return
    }
    Job.update({ isDel: 1 }, { where: { uid: uid, id: req.query.id, isDel: 0 } }).then((r => {
        // res.json(result(0,'success','删除成功'))
        res.json(result(0, 'success', '删除成功'))
    })).catch(e => {
        res.json(result(-1, 'fail', '删除失败'))
    })
}
/**
 * 添加技能
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.skill_add = async (req, res) => {
    let uid = req.query.uid;
    let _name = req.query.name;
    let level = req.query.level;
    if (!uid || !_name || !level) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Skill.create({
        uid: uid,
        name: _name,
        level: level
    }).then(v => {
        res.json(result(0, 'success', '创建成功'))
    }).catch(e => {
        res.json(result(-1, 'fail', '创建失败'))
    })
}

/**
 * 删除技能
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.skill_delete = async (req, res) => {
    let uid = req.query.uid;
    let id = req.query.id;
    if (!uid || !id) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Skill.update({ isDel: 1 }, {
        where: {
            id: id,
            isDel: 0,
            uid: uid
        }
    }).then(v => {
        res.json(result(0, 'success', '删除成功'))
    }).catch(e => {
        res.json(result(-1, 'fail', '删除失败'))
    })
}

/**
 * 技能查询
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.skill_show = async (req, res) => {
    let uid = req.query.uid;
    if (!uid) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Skill.findAll({
        where: {
            isDel: 0,
            uid: uid
        }
    }).then(v => {
        res.json(result(0, 'success', v))
    }).catch(e => {
        res.json(result(-1, 'fail', '查询失败'))
    })
}
/**
 * 添加作品
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.opus_add = async (req, res) => {
    let uid = req.query.uid;
    let _name = req.query.name;
    let links = req.query.links;
    let image1 = req.query.image1;
    let image2 = req.query.image2;
    let des = req.query.des;

    let level = req.query.level;
    if (!uid || !_name || !links || !des || !image1) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Opus.create({
        uid: uid,
        name: _name,
        image1: image1,
        image2: image2,
        links: links,
        des: des,
    }).then(v => {
        res.json(result(0, 'success', '创建成功'))
    }).catch(e => {
        res.json(result(-1, 'fail', '创建失败'))
    })
}

/**
 * 删除作品
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.opus_delete = async (req, res) => {
    let uid = req.query.uid;
    let id = req.query.id
    if (!uid || !id) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Opus.update({ isDel: 1 }, { where: { id: id, isDel: 0 } }).then(v => {

        res.json(result(0, 'success', '删除成功'))
    }).catch(e => {
        res.json(result(-1, 'fail', '删除失败'))
    })
}
/**
 * 查询作品
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.opus_show = async (req, res) => {
    let uid = req.query.uid;
    if (!uid) {
        res.json(result(-1, 'fail', '缺少参数'))
        return
    }
    Opus.findAll({ where: { uid: uid, isDel: 0 } }).then(v => {
        res.json(result(0, 'success', v))
    }).catch(e => {
        res.json(result(-1, 'fail', '查询失败'))
    })
}
